package com.ty.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.mapper.HeartMethodMapper;
import com.ty.pojo.HeartMethod;
import com.ty.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HeartMethodServiceImpl implements HeartMethodService{
    @Autowired
    private HeartMethodMapper heartMethodMapper;


    @Override
    public PageResult getHeartMethodList(PageResult pageResult, HeartMethod heartMethod) {
        IPage<HeartMethod> iPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<HeartMethod> queryWrapper = new QueryWrapper<>();
        boolean getName = StringUtils.hasLength(heartMethod.getHeartName());
        boolean getQuality;
        if (heartMethod.getHeartQuality() == null || "".equals(heartMethod.getHeartQuality())){
            getQuality = false;
        }else{
            getQuality = true;
        }
        queryWrapper.and(Wrapper -> Wrapper.like(getName,"heart_name", heartMethod.getHeartName())
                .eq(getQuality,"heart_quality",heartMethod.getHeartQuality())
                .eq("state", 1));
        iPage = heartMethodMapper.selectPage(iPage, queryWrapper);
        List<HeartMethod> records = iPage.getRecords();
        long total = iPage.getTotal();
        return pageResult.setTotal(total).setRows(records);
    }

    @Override
    public void updateState(Integer id) {
        UpdateWrapper<HeartMethod> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        HeartMethod heartMethod = new HeartMethod();
        heartMethod.setState(0);
        heartMethodMapper.update(heartMethod, updateWrapper);
    }

    //新增数据
    @Override
    public void insertHeartMethod(HeartMethod heartMethod) {
        heartMethod.setState(1);
        heartMethodMapper.insert(heartMethod);
    }

    @Override
    public void updateHeartMethod(HeartMethod heartMethod) {
        heartMethodMapper.updateById(heartMethod);
    }

}
